/**********************************************************************************
Program:       chinaweathergadget

License:       GNU/GPL version 3

File:          Form_Main.Designer.2.cs

Description:   绘制GUI，包含Main函数

Author:        Guangyu Liu （lgy0417@gmail.com）

Environment:   .NET Framework v2.0 or later version

Usage/Example: README_chs.txt

Last Modified: 2010.08.30

***********************************************************************************/


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using WeatherInfo;


namespace GUI
{
	public partial class MainForm
	{
		#region data-related events
		
		private void UpdateCurrentCondition(object sender, EventArgs e)
		{
			observationTimeValue.Text = "-";
			currentTempValue.Text = "-";
			windDirectionValue.Text = "-";
			windSpeedValue.Text = "-";
			humidityValue.Text = "-";
			airPressureValue.Text = "-";
			try
			{
				cc.CurrentCityID = cc.CurrentCityID;   // 自动更新，城市不变
				observationTimeValue.Text = cc.CurrentObservationTime;
				currentTempValue.Text = cc.CurrentTemp + " \u00B0" + "C";
				windDirectionValue.Text = cc.CurrentWindDirection;
				windSpeedValue.Text = cc.CurrentWindSpeed + " m/s";
				humidityValue.Text = cc.CurrentHumidity;
				airPressureValue.Text = cc.CurrentAirPressure;
				sysTray.Text = currentCityName + "  " + cc.CurrentTemp + " \u00B0" + "C";
				sysTray.BalloonTipTitle = "实时天气信息更新完毕！";
				sysTray.BalloonTipText = currentCityName + "  " + cc.CurrentTemp + " \u00B0" + "C\n观测时间： " + cc.CurrentObservationTime;
				sysTray.ShowBalloonTip(2000);
			}
			catch (Exception)
			{
				MessageBox.Show("自动更新出现错误，请检查您的网络连接！", 
								"运行错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		private void UpdateGlobally(object sender, EventArgs e)
		{
			currentCityName = cbu.CityList[cityNameComboBox.SelectedIndex].Name;
			bool success = true;
			this.SuspendLayout();
			try
			{
				cc.CurrentCityID = cbu.CityList[cityNameComboBox.SelectedIndex].CityID.ToString();
				wf.CityID = cc.CurrentCityID;
			}
			catch (System.Net.WebException)
			{
				MessageBox.Show("请检查您的网络连接！", 
								"运行错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				success = false;
			}
			catch (WrongCityNameException)
			{
				string str = "当前城市：" + cbu.CityList[cityNameComboBox.SelectedIndex].Name + "  不能获取实时天气数据，请联系软件作者反映此问题！";
				MessageBox.Show(str, "运行错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				success = false;
			}
			catch (Exception)
			{
				MessageBox.Show("未知运行错误，请反馈给软件作者！", 
								"运行错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				success = false;
			}
			finally
			{
				GlobalDisplay(success);
				this.ResumeLayout();
			}
		}

		private void GlobalDisplay(bool success)
		{
			if(!success)      // 更新失败
			{
				if(!isStartUp)
					cityNameComboBox.Text = previousCityName;
				else
				{
					observationTimeValue.Text = "-";
					currentTempValue.Text = "-";
					windDirectionValue.Text = "-";
					windSpeedValue.Text = "-";
					humidityValue.Text = "-";
					airPressureValue.Text = "-";
				}
				return;
			}

			previousCityName = currentCityName;
			if (isStartUp)
				isStartUp = false;
			
			int index = wf.ForecastInfo[wf.ForecastInfoIndex].LastIndexOf(' ');
			string lunarCalendar = wf.ForecastInfo[wf.ForecastInfoIndex].Insert(index + 1, "农历");
			this.Text = "中国城市天气信息 " + version + " - " + lunarCalendar;
			
			#region 实时天气
			observationTimeValue.Text = cc.CurrentObservationTime;
			currentTempValue.Text = cc.CurrentTemp + " \u00B0" + "C";
			windDirectionValue.Text = cc.CurrentWindDirection;
			windSpeedValue.Text = cc.CurrentWindSpeed + " m/s";
			humidityValue.Text = cc.CurrentHumidity;
			airPressureValue.Text = cc.CurrentAirPressure;
			#endregion
			
			#region 1-3天预报
			
			f3_TimeOfPublishing.Text = wf.ForecastInfo[wf.ForecastInfoIndex + 16] + " " + wf.ForecastInfo[wf.ForecastInfoIndex + 17];
			indexOfForecast_3_Day1 = wf.Has6Forecasts ? wf.ForecastInfoIndex + 25 : wf.ForecastInfoIndex + 31;
			
			f3_Day1.Text = wf.ForecastInfo[indexOfForecast_3_Day1];
			f3_Day1_DayTime.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 1];
			f3_Day1_DayTime_Weather.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 2];
			f3_Day1_DayTime_Temp.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 3];
			f3_Day1_DayTime_WindDirection.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 4];
			f3_Day1_DayTime_WindStrength.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 5];
			f3_Day1_Night.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 6];
			f3_Day1_Night_Weather.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 7];
			f3_Day1_Night_Temp.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 8];
			f3_Day1_Night_WindDirection.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 9];
			f3_Day1_Night_WindStrength.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 10];
			
			f3_Day2.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 11];
			f3_Day2_DayTime.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 12];
			f3_Day2_DayTime_Weather.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 13];
			f3_Day2_DayTime_Temp.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 14];
			f3_Day2_DayTime_WindDirection.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 15];
			f3_Day2_DayTime_WindStrength.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 16];
			f3_Day2_Night.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 17];
			f3_Day2_Night_Weather.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 18];
			f3_Day2_Night_Temp.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 19];
			f3_Day2_Night_WindDirection.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 20];
			f3_Day2_Night_WindStrength.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 21];
			
			f3_Day3.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 22];
			f3_Day3_DayTime.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 23];
			f3_Day3_DayTime_Weather.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 24];
			f3_Day3_DayTime_Temp.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 25];
			f3_Day3_DayTime_WindDirection.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 26];
			f3_Day3_DayTime_WindStrength.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 27];
			f3_Day3_Night.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 28];
			f3_Day3_Night_Weather.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 29];
			f3_Day3_Night_Temp.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 30];
			f3_Day3_Night_WindDirection.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 31];
			f3_Day3_Night_WindStrength.Text = wf.ForecastInfo[indexOfForecast_3_Day1 + 32];
			
			if(!wf.Has6Forecasts)
				f3_Day0_Night.Text = "今天" 
										   + wf.ForecastInfo[wf.ForecastInfoIndex + 26]
										   + wf.ForecastInfo[wf.ForecastInfoIndex + 27]
										   + "、"
										   + wf.ForecastInfo[wf.ForecastInfoIndex + 30]
										   + "，最"
										   + wf.ForecastInfo[wf.ForecastInfoIndex + 28];
			else
				f3_Day0_Night.Hide();

			#endregion

			#region 4-7天预报

			indexOfForecast_7 = wf.Has6Forecasts ? wf.ForecastInfoIndex + 58 : wf.ForecastInfoIndex + 64;
			f7_TimeOfPublishing.Text = currentCityName + wf.ForecastInfo[indexOfForecast_7];

			f7_Day4.Text = wf.ForecastInfo[indexOfForecast_7 + 1];
			f7_Day4_DayTime.Text = wf.ForecastInfo[indexOfForecast_7 + 2];
			f7_Day4_DayTime_Weather.Text = wf.ForecastInfo[indexOfForecast_7 + 3];
			f7_Day4_DayTime_Temp.Text = wf.ForecastInfo[indexOfForecast_7 + 4];
			f7_Day4_DayTime_WindDirection.Text = wf.ForecastInfo[indexOfForecast_7 + 5];
			f7_Day4_DayTime_WindStrength.Text = wf.ForecastInfo[indexOfForecast_7 + 6];
			f7_Day4_Night.Text = wf.ForecastInfo[indexOfForecast_7 + 7];
			f7_Day4_Night_Weather.Text = wf.ForecastInfo[indexOfForecast_7 + 8];
			f7_Day4_Night_Temp.Text = wf.ForecastInfo[indexOfForecast_7 + 9];
			f7_Day4_Night_WindDirection.Text = wf.ForecastInfo[indexOfForecast_7 + 10];
			f7_Day4_Night_WindStrength.Text = wf.ForecastInfo[indexOfForecast_7 + 11];

			f7_Day5.Text = wf.ForecastInfo[indexOfForecast_7 + 12];
			f7_Day5_DayTime.Text = wf.ForecastInfo[indexOfForecast_7 + 13];
			f7_Day5_DayTime_Weather.Text = wf.ForecastInfo[indexOfForecast_7 + 14];
			f7_Day5_DayTime_Temp.Text = wf.ForecastInfo[indexOfForecast_7 + 15];
			f7_Day5_DayTime_WindDirection.Text = wf.ForecastInfo[indexOfForecast_7 + 16];
			f7_Day5_DayTime_WindStrength.Text = wf.ForecastInfo[indexOfForecast_7 + 17];
			f7_Day5_Night.Text = wf.ForecastInfo[indexOfForecast_7 + 18];
			f7_Day5_Night_Weather.Text = wf.ForecastInfo[indexOfForecast_7 + 19];
			f7_Day5_Night_Temp.Text = wf.ForecastInfo[indexOfForecast_7 + 20];
			f7_Day5_Night_WindDirection.Text = wf.ForecastInfo[indexOfForecast_7 + 21];
			f7_Day5_Night_WindStrength.Text = wf.ForecastInfo[indexOfForecast_7 + 22];

			f7_Day6.Text = wf.ForecastInfo[indexOfForecast_7 + 23];
			f7_Day6_DayTime.Text = wf.ForecastInfo[indexOfForecast_7 + 24];
			f7_Day6_DayTime_Weather.Text = wf.ForecastInfo[indexOfForecast_7 + 25];
			f7_Day6_DayTime_Temp.Text = wf.ForecastInfo[indexOfForecast_7 + 26];
			f7_Day6_DayTime_WindDirection.Text = wf.ForecastInfo[indexOfForecast_7 + 27];
			f7_Day6_DayTime_WindStrength.Text = wf.ForecastInfo[indexOfForecast_7 + 28];
			f7_Day6_Night.Text = wf.ForecastInfo[indexOfForecast_7 + 29];
			f7_Day6_Night_Weather.Text = wf.ForecastInfo[indexOfForecast_7 + 30];
			f7_Day6_Night_Temp.Text = wf.ForecastInfo[indexOfForecast_7 + 31];
			f7_Day6_Night_WindDirection.Text = wf.ForecastInfo[indexOfForecast_7 + 32];
			f7_Day6_Night_WindStrength.Text = wf.ForecastInfo[indexOfForecast_7 + 33];

			f7_Day7.Text = wf.ForecastInfo[indexOfForecast_7 + 34];
			f7_Day7_DayTime.Text = wf.ForecastInfo[indexOfForecast_7 + 35];
			f7_Day7_DayTime_Weather.Text = wf.ForecastInfo[indexOfForecast_7 + 36];
			f7_Day7_DayTime_Temp.Text = wf.ForecastInfo[indexOfForecast_7 + 37];
			f7_Day7_DayTime_WindDirection.Text = wf.ForecastInfo[indexOfForecast_7 + 38];
			f7_Day7_DayTime_WindStrength.Text = wf.ForecastInfo[indexOfForecast_7 + 39];

			#endregion

			#region 生活指数

			indexOfShengHuoZhiShu = wf.Has6Forecasts ? wf.ForecastInfoIndex + 100 : wf.ForecastInfoIndex + 106;
			shzs_TimeOfPublishing.Text = wf.ForecastInfo[indexOfShengHuoZhiShu].Insert(2, currentCityName);
			shzs_UsageTip.Text = "（提示：鼠标悬停于任意指数之上可获得更多信息）";

			shzs_ZiWaiXian.Text = "紫外线指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 2];
			shzs_WuRanKuoSan.Text = "污染扩散指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 5];
			shzs_ChuanYi.Text = "穿衣指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 8];
			shzs_ChenLian.Text = "晨练指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 11];
			shzs_ShuShiDu.Text = "舒适度指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 14];
			shzs_GanMao.Text = "感冒指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 17];
			shzs_JiaoTong.Text = "交通指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 20];			
			shzs_XiChe.Text = "洗车指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 23];
			shzs_LiangShai.Text = "晾晒指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 26];
			shzs_YunDong.Text = "运动指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 29];
			shzs_LvYou.Text = "旅游指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 32];
			shzs_HuaZhuang.Text = "化妆指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 35];
			shzs_DiaoYu.Text = "钓鱼指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 38];
			shzs_YueHui.Text = "约会指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 41];
			shzs_YuSan.Text = "雨伞指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 44];
			shzs_LuKuang.Text = "路况指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 47];
			shzs_HuaChuan.Text = "划船指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 50];
			shzs_YeShengHuo.Text = "夜生活指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 53];
			shzs_GuangJie.Text = "逛街指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 56];
			shzs_MeiFa.Text = "美发指数：" + wf.ForecastInfo[indexOfShengHuoZhiShu + 59];

			#endregion

			#region 生活指数ToolTip提示

			toolTip1.AutoPopDelay = 10000;
			toolTip1.InitialDelay = 300;
			toolTip1.ReshowDelay = 500;
			toolTip1.ShowAlways = true;
			toolTip1.IsBalloon = true;
			toolTip1.BackColor = Color.FromArgb(241, 242 ,247);
			toolTip1.RemoveAll();

			toolTip1.SetToolTip(shzs_ZiWaiXian, wf.ForecastInfo[indexOfShengHuoZhiShu + 3]);
			toolTip1.SetToolTip(shzs_WuRanKuoSan, wf.ForecastInfo[indexOfShengHuoZhiShu + 6]);
			toolTip1.SetToolTip(shzs_ChuanYi, wf.ForecastInfo[indexOfShengHuoZhiShu + 9]);
			toolTip1.SetToolTip(shzs_ChenLian, wf.ForecastInfo[indexOfShengHuoZhiShu + 12]);
			toolTip1.SetToolTip(shzs_ShuShiDu, wf.ForecastInfo[indexOfShengHuoZhiShu + 15]);
			toolTip1.SetToolTip(shzs_GanMao, wf.ForecastInfo[indexOfShengHuoZhiShu + 18]);
			toolTip1.SetToolTip(shzs_JiaoTong, wf.ForecastInfo[indexOfShengHuoZhiShu + 21]);
			toolTip1.SetToolTip(shzs_XiChe, wf.ForecastInfo[indexOfShengHuoZhiShu + 24]);
			toolTip1.SetToolTip(shzs_LiangShai, wf.ForecastInfo[indexOfShengHuoZhiShu + 27]);
			toolTip1.SetToolTip(shzs_YunDong, wf.ForecastInfo[indexOfShengHuoZhiShu + 30]);
			toolTip1.SetToolTip(shzs_LvYou, wf.ForecastInfo[indexOfShengHuoZhiShu + 33]);
			toolTip1.SetToolTip(shzs_HuaZhuang, wf.ForecastInfo[indexOfShengHuoZhiShu + 36]);
			toolTip1.SetToolTip(shzs_DiaoYu, wf.ForecastInfo[indexOfShengHuoZhiShu + 39]);
			toolTip1.SetToolTip(shzs_YueHui, wf.ForecastInfo[indexOfShengHuoZhiShu + 42]);
			toolTip1.SetToolTip(shzs_YuSan, wf.ForecastInfo[indexOfShengHuoZhiShu + 45]);
			toolTip1.SetToolTip(shzs_LuKuang, wf.ForecastInfo[indexOfShengHuoZhiShu + 48]);
			toolTip1.SetToolTip(shzs_HuaChuan, wf.ForecastInfo[indexOfShengHuoZhiShu + 51]);
			toolTip1.SetToolTip(shzs_YeShengHuo, wf.ForecastInfo[indexOfShengHuoZhiShu + 54]);
			toolTip1.SetToolTip(shzs_GuangJie, wf.ForecastInfo[indexOfShengHuoZhiShu + 57]);
			toolTip1.SetToolTip(shzs_MeiFa, wf.ForecastInfo[indexOfShengHuoZhiShu + 60]);

			toolTip1.SetToolTip(refresh, "单击以刷新所有天气数据");
			toolTip1.SetToolTip(exitButton, "单击以退出程序");
			toolTip1.SetToolTip(editCityListButton, "单击以编辑城市列表");

			#endregion

			#region 系统托盘
			sysTray.Text = currentCityName + "  " + cc.CurrentTemp + " \u00B0" + "C";
			sysTray.BalloonTipTitle = "实时天气信息更新完毕！";
			sysTray.BalloonTipText = currentCityName + "  " + cc.CurrentTemp + " \u00B0" + "C\n观测时间： " + cc.CurrentObservationTime;
			sysTray.ShowBalloonTip(2000);
			#endregion
		}

		private void EditCities(object sender, EventArgs e)
		{
			// “编辑”按钮被按下时执行
			// 弹出编辑城市列表的窗体，传入当前 cityNameComboBox 中的内容以及 CityNameListModified 函数的委托
			if (!idDatabase_loaded)
			{
				idDatabase.ReadData();
				idDatabase_loaded = true;
			}
			CityListChangedDelegate clcd = new CityListChangedDelegate(this.CityListModified);
			EditCitiesForm ecForm = new EditCitiesForm(cbu.CityList, idDatabase, clcd);
			ecForm.ShowDialog();
		}
		
		private void CityListModified(List<SingleCity> newCities)
		{
			// 此函数由 EditCitiesForm 类定义的子窗体 ecForm 回调，参见本文件中的 EditCities 方法
			// 替换为 cbu.CityList，然后在 cityNameComboBox 的下拉列表中更新
			// 但是保证 cityNameComboBox.Text 不变化
			cbu.CityList = newCities;
			string tmp = cityNameComboBox.Text;
			cityNameComboBox.Items.Clear();
			string[] range = cbu.GetCityNameRange();
			cityNameComboBox.Items.AddRange(range);
			for (int i = 0; i < range.Length; i++)
				if (tmp == range[i])
					cityNameComboBox.SelectedIndex = i;
		}
		
		#endregion

		#region GUI-related events
		
		private void Form_SizeChanged(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
			{
				this.ShowInTaskbar = false;
				menuItemOpen.Text = "打开";
			}
			else
			{
				this.ShowInTaskbar = true;
				menuItemOpen.Text = "隐藏";
			}
		}
		
		private void Form_Closing(object sender, FormClosingEventArgs e)
		{
			// 判断城市列表与最初始状态是否一致，如不一致询问是否保存
			int i = -1;
			if (cbu.OriginalList.Count == cbu.CityList.Count)
				for (i = 0; i < cbu.OriginalList.Count; i++)
					if (cbu.OriginalList[i].CityID != cbu.CityList[i].CityID)
						break;
			if (i != cbu.OriginalList.Count)
			{
				DialogResult result = MessageBox.Show("您的自定义城市列表已经更改。是否要保存此修改？", "提示", 
													MessageBoxButtons.YesNoCancel,  MessageBoxIcon.Question);
				if(result == System.Windows.Forms.DialogResult.Cancel)
				{
					e.Cancel = true;
					this.Show();    // 取消 Form_Close 函数中的 Hide()
					return;
				}
				else if(result == System.Windows.Forms.DialogResult.Yes)
					cbu.Save();
			}
			sysTray.Visible = false;
		}

		private void Form_Close(object Sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
				this.Hide();      // 最小化从托盘关闭时不再弹出主窗口，直接退出
			this.Close();
		}
		
		private void Form_Recover(object Sender, EventArgs e)
		{
			sysTray_DoubleClick(null, null);
		}

		private void sysTray_DoubleClick(object Sender, MouseEventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
			{
				this.WindowState = FormWindowState.Normal;
				this.Activate();
			}
			else
				this.WindowState = FormWindowState.Minimized;
		}
		
		private void About(object Sender, EventArgs e)
		{
			AboutForm af = new AboutForm();
			af.ShowDialog();
		}

		#endregion
	}
}